package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Business;
import cn.wolfcode.qo.BusinessReportQueryObject;
import cn.wolfcode.service.IBusinessReportService;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.util.BusinessReportEnum;
import cn.wolfcode.util.ConsumptionStatusEnum;
import cn.wolfcode.util.RequiredPermission;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/businessReport")
public class BusinessReportController {
    @Autowired
    private IBusinessReportService businessReportService;
    @Autowired
    private IBusinessService businessService;


    @RequestMapping("/list")
    @RequiredPermission(name = "门店报表页面", expression = "businessReport:list")
    public String list(@ModelAttribute("qo") BusinessReportQueryObject qo, Model model) {
        model.addAttribute("businesses", businessService.selectBusinessList());
        model.addAttribute("enums", ConsumptionStatusEnum.values());
        model.addAttribute("reportEnums", BusinessReportEnum.values());
        PageInfo pageInfo = businessReportService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "businessReport/list";
    }

    @RequiredPermission(name = "部门收入柱状图报表", expression = "businessReport:listBar")
    @RequestMapping("/listBar")
    public String listBar(Model model, @ModelAttribute("qo") BusinessReportQueryObject qo) {
        ArrayList<Object> groupTypeList = new ArrayList<>();
        ArrayList<Object> numberList = new ArrayList<>();
        ArrayList<Object> totalAmountList = new ArrayList<>();
        ArrayList<Object> payAmountList = new ArrayList<>();
        ArrayList<Object> discountAmountList = new ArrayList<>();
        //设置不分页
        qo.setPageSize(0);
        PageInfo<HashMap> pageInfo = businessReportService.query(qo);
        //转换为list集合
        List<HashMap> list = pageInfo.getList();
        //遍历每一行的hashMap对象 存到各种类型的list里面
        for (HashMap map : list) {
            groupTypeList.add(map.get("groupType"));
            numberList.add(map.get("number"));
            totalAmountList.add(map.get("totalAmount"));
            payAmountList.add(map.get("payAmount"));
            discountAmountList.add(map.get("discountAmount"));
        }
        //共享到页面上 因为页面接收的数据要有格式 所以需要转格式
        model.addAttribute("groupTypeList", JSON.toJSONString(groupTypeList));
        model.addAttribute("numberList", JSON.toJSONString(numberList));
        model.addAttribute("totalAmountList", JSON.toJSONString(totalAmountList));
        model.addAttribute("payAmountList", JSON.toJSONString(payAmountList));
        model.addAttribute("discountAmountList", JSON.toJSONString(discountAmountList));
        return "businessReport/listBar";
    }


}
